<?php
/**
 * TODO check if an item previously created could be reused
 *
 */


abstract class Yz_Controller_Templated
    extends Yz_Controller_Abstract
{

    protected $viewClass = 'Yz_View_Template';
    protected $template;
    protected $templateName;

    /** null: use default layout [or admin layout in admin mode]
     *  false: prevent use of layout
     *  @var string|null|false
     */
    protected $layoutName;


    /** shortcut to determine template, populate it with values and assign it to the view
     *  @return this
     *  @throw
     */
    protected function output()
    {
        $template = $this->selectTemplate();
        return $this
            ->assignTemplateParams($template)
            ->outputTemplate($template)
        ;
    }

    /** assign custom parameters to template
     *  @param Yz_Template_Interface $template
     *  @return this
     *  @throw Nothing
     */
    protected function assignTemplateParams(Yz_Template_Interface $template)
    {
        return $this;
    }


    /** select a template for output
     *      On first call, create
     *  @param string[null] $name
     *  @return Yz_Template_Interface
     */
    protected function selectTemplate($name=null)
    {
        debug();
        $t =& $this->template;
        if (null === $t) {
            if (null === $name) {
                $name = $this->templateName
                    or $name = $this->name;
            }
            $t = $this->module->selectTemplate($name);
        }
        return $t;
    }


    /** assign common parameters to template, select layout and supply them to the view
     *  @param Yz_Template_Interface|null $template
     *  @return this
     *
     */
    protected function outputTemplate(Yz_Template_Interface $template=null)
    {
        if (null === $template)
            $template = $this->selectTemplate();

        // assign variables to template
        if ($this->isAdmin())
            $admin = Yz::selectModule('Admin')->getWebPath();
        else
            $admin = "";

        $template->YzAdmin = $admin;
        $template->webRoot   = $this->getRequest()->getWebRoot();
        $template->webPrefix = $this->getRequest()->getWebPrefix();
        $template->YzModule  = Yz_String::fromCamelCase($this->module->getName());
        #$template->YzModuleHref = $p . $admin . $m . '/';
        $template->YzModuleHref = $this->module->getWebPath();
        //$template->YzOperation = $router->getControllerSelector();
        $template->YzLanguage = I18N::getCurrentLanguage();

        $view = $this->getView();
        $layout = $this->selectLayout()
            and $view->selectLayout($layout);
        $view->setTemplate($template) ;
        return $this;
    }


    /** select layout name based on this.layoutName and admin mode
     *  @return string|false
     */
    protected function selectLayout()
    {
        debug();
        $name = $this->layoutName;
        if (null === $name)
            $name = $this->isAdmin() ? 'Admin' : 'Default';
        return $name;
    }

}
